﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WBFSe3.Streams
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public delegate void StreamClosedDelegate(WbfsStream stream);


    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public abstract class WbfsStream : Stream
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        protected int sectorSize;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public abstract String Name { get; }
        public int SectorSize { get { return this.sectorSize; } }


        //-------------------------------------------------------------------------------------------------------
        //
        //-------------------------------------------------------------------------------------------------------
        public event StreamClosedDelegate StreamClosed;


        //-------------------------------------------------------------------------------------------------------
        //
        //-------------------------------------------------------------------------------------------------------
        protected void OnStreamClosed()
        {
            if (this.StreamClosed != null)
                this.StreamClosed(this);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsStream(int sectorSize)
        {
            this.sectorSize = sectorSize;
        }


        //-------------------------------------------------------------------------------------------------------
        //
        //-------------------------------------------------------------------------------------------------------
        public abstract int ReadSector(int sector, IntPtr array, int offset);


        //-------------------------------------------------------------------------------------------------------
        //
        //-------------------------------------------------------------------------------------------------------
        public abstract int WriteSector(int sector, IntPtr array, int offset);


        //-------------------------------------------------------------------------------------------------------
        //
        //-------------------------------------------------------------------------------------------------------
        public abstract void WriteEmptyBlock(int count);


        //-------------------------------------------------------------------------------------------------------
        //
        //-------------------------------------------------------------------------------------------------------
        public override void Close()
        {
            OnStreamClosed();
            base.Close();
        }
    }
}
